﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Bus_proj.website
{
    public partial class Add_type_bus : System.Web.UI.Page
    {
        int z;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    if (Int32.Parse(Session["login_Access_id"].ToString()) == 3)
                    {
                        GridView1.DataBind();
                    }
                    else
                    {
                        Response.Redirect("login.aspx");
                    }
                }
                catch (Exception)
                {
                    Response.Redirect("login.aspx");
                }                
            }
            
        }
        
        protected void Button1_Click(object sender, EventArgs e)
        {
            DataClasses1DataContext db = new DataClasses1DataContext();
            try
            {
                Select SData = new Select();
                Table_Type_Bus x = new Table_Type_Bus();
                z = SData.GetId_type_bus();
                x.Type_Bus_id = z;
                x.Type_Bus_name = TextBox1.Text;
                x.Type_Bus_seat_colum = Int32.Parse(TextBox2.Text);
                x.Type_Bus_seat_row = Int32.Parse(TextBox3.Text);
                x.Type_Bus_number = Int32.Parse(TextBox2.Text) * Int32.Parse(TextBox3.Text);                          
                db.Table_Type_Bus.InsertOnSubmit(x);
                db.SubmitChanges();
                GridView1.DataBind();
                //BindingGrid();
                //ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('เพิ่มประเภทรถเรียบร้อย');", true);
                ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('เพิ่มประเภทรถเรียบร้อย');", true);
                //Response.Write(@"<script language='javascript'>alert('เพิ่มข้อมูลลูกค้าแล้ว');</script>");
            }
            catch (Exception)
            {
                ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('ไม่สามารถเพิ่มประเภทได้');", true);
                //Response.Write(@"<script language='javascript'>alert('ไม่สามารถเพิ่มข้อมูลลูกค้าได้');</script>");
                throw;
            }
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int a;
            DataClasses1DataContext db = new DataClasses1DataContext();
            if (e.CommandName == "cmdY")
            {
                a = Convert.ToInt32(e.CommandArgument.ToString());
                var q = from p in db.Table_Type_Bus
                        where p.Type_Bus_id == a
                        select p;
                try
                {
                    db.Table_Type_Bus.DeleteAllOnSubmit(q);
                    db.SubmitChanges();
                    GridView1.DataBind();
                    ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('ลบจุดจอดเสร็จสิ้น');", true);
                    //Response.Write(@"<script language='javascript'>alert('ลบจุดจอดเสร็จสิ้น');</script>");
                    //BindingGrid();
                }
                catch (Exception)
                {
                    ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('ไม่สามารถลบจุดจอดได้');", true);
                    //Response.Write(@"<script language='javascript'>alert('ลบจุดจอดเสร็จสิ้น');</script>");
                    //throw;
                    //BindingGrid();
                }
            }   
        }
    }
}